<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
//应用商城
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$types=get_permission_types('id','all');
$tabs=array();
foreach($types as $row){
	$tabs[]=array('op'=>'list','query'=>'type='.$row['id'],'title'=>$row['title'],'type'=>$row['id']);
}
$tabs[]=array('op'=>'buy_num','title'=>'应用个数购买');
$return=array();
if($_RQ['op']=='list'){
	$type=intval($_RQ['type']);
	$group_id=intval($_RQ['group_id']);
	$style=intval($_RQ['style']);
	$return['params']['type']=$type;
	$return['params']['group_id']=$group_id;
	$return['params']['style']=$style;
	$return['params']['hide']=0;
	if(!$type){
		//系统功能
		$params=array('type'=>array('1','2','3'));
	}elseif(in_array($type,array('2','3','5'))){
		//手机站模版、pc站模版、小程序模版
		$params=array('uniacid'=>0,'hide'=>0);
	}else{
		$params=array('hide'=>0);
	}
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	
	$fields=array('id','title','price','days','image','tuijie','hdps','paixu');
	if(in_array($type,array('2','3','5'))){
		$fields[]='group_id';
		$fields[]='salenum';
		if($_RQ['group_id']){
			$params['group_id']=$group_id;
		}
	}
	if($type==1){
		//插件功能
		$fields[]='oneword';
		$fields[]='salenum';
		if($_RQ['style']){
			$params['style']=$style;
		}
	}
	$orderby='`paixu` desc, `tuijie` desc ,`price` desc, `id` desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 58;
	$total='';
	$list=pdo_getslice($types[$type]['table'],$params,array($pindex,$psize),$total,$fields,'',$orderby);
	if($list){
		$group_actions=pdo_getcolumn('core_account_group',array('id'=>$_SESSION['account']['groupid']),$types[$type]['name']);
		foreach($list as &$row){
			$row['oldprice']=$row['price']>0?price($row['price']*1.6):'108.00';
			if($row['price']<1){
				$row['time']='免费';
			}elseif(strexists($group_actions,';'.$row['id'].';')){
				$row['time']='套餐内置';
			}else{
				$row['buy']=1;
				$time=pdo_getcolumn('core_account_permissions',array('uniacid'=>$_SESSION['uniacid'],'type'=>$type,'pid'=>$row['id']),'time');
				if(!$time){
					$row['time']="待购买";
					$row['buy_action']=array('op'=>'buy','query'=>'type='.$type.'&id='.$row['id'],'title'=>'购买应用');
				}elseif($time<=TIMESTAMP){
					$row['time']='已过期';
					$row['buy_action']=array('op'=>'buy','query'=>'type='.$type.'&id='.$row['id'],'title'=>'购买应用');
				}else{
					$row['time']='有效期至：'.date('20y/m/d',$time);
					$row['buy_action']=array('op'=>'buy','query'=>'type='.$type.'&id='.$row['id'],'title'=>'续费应用');
				}
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['groups']=pdo_getall('core_template_group',array(),array('id','title'));
	if($type==1){
		$return['groups']=get_plugin_styles();
	}
	/*$return['htmls']=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','delall'=>false,'del_one'=>false,'edit'=>false,'list'=>array(
			array('type'=>'string','name'=>'title','title'=>'应用名称'),
			array('type'=>'string','name'=>'image','title'=>'图片'),
			array('type'=>'string','name'=>'price','title'=>'应用价格'),
			array('type'=>'string','name'=>'days','title'=>'价格周期'),
			array('type'=>'string','name'=>'title','title'=>'应用状态'),
		),'action'=>array(
			array('type'=>'href','name'=>'buy_action','show'=>array('buy',1)),
		)),
		'search_one'=>array('name'=>'title'),
	);*/
	$return['type'] =$type;
	$return['tabs']=$tabs;
	exi($return);
}

if($_RQ['op']=='buy'){
	$type=intval($_RQ['type']);
	$id=intval($_RQ['id']);
	if(!$id || !isset($_RQ['type'])){
		exi('页面参数错误！请先选择要购买的应用和应用类型','error');
	}
	if(!$_SESSION['uid']){
		exi('请先登录！','error','user.login/');
	}
	$item=pdo_get($types[$type]['table'],array('id'=>$id),array('id','title','price','days'));
	$item['type']=$type;
	$item['type_title']=$types[$type]['title'];
	$item['price']="{$item['price']}元/{$item['days']}天";
	$return['item']=$item;
	$nums=array(
		'1'=>($item['days']*1).'天',
		'2'=>($item['days']*2).'天',
		'3'=>($item['days']*3).'天',
		'4'=>($item['days']*4).'天',
		'5'=>($item['days']*5).'天',
	);
	$return['nums']=$nums;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'设置购买信息',
		'post'=>'buy_post',
		'edit'=>array(
			array('type'=>'string','name'=>'title','title'=>'应用名称','read'=>true),
			array('type'=>'string','name'=>'type_title','title'=>'应用类型','read'=>true),
			array('type'=>'string','name'=>'price','title'=>'应用价格','read'=>true),
			array('type'=>'radio','name'=>'num','title'=>'购买天数','options'=>'nums'),
		),
	);
	exi($return);
}
if($_RQ['op']=='buy_post'){
	if($_SESSION['isfounder']){
		exi('您是超级管理员，无需购买！','error');
	}
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('请先选择应用！','error');
	}
	$num=intval($post['num']);
	if(!$num){
		exi('请先选择购买时长！','error');
	}
	$type=intval($post['type']);
	$action=pdo_get($types[$type]['table'],array('id'=>$id),array('id','price','days','title'));
	$fee=$action['price']*$num;
	$days=$action['days']*$num;
	/*$credit=pdo_getcolumn('core_accounts',array('uniacid'=>$_SESSION['uniacid']),'credit2');
	if($credit<$fee){
		exi('账户余额不足！请先充值！','error','core/account.recharge/');
	}
	$pay=array(
		'type'=>2,
		'is_add'=>0,
		'fee'=>$fee,
		'remark'=>"购买【{$types[$type]['title']}】:{$action['title']}",
	);
	model('core/web')->account_credit($pay);*/
	
	$old=pdo_get('core_account_permissions',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$action['id'],'type'=>$type),array('id','time'));
	$store_log=array(
		'uniacid'=>$_SESSION['uniacid'],
		'pid'=>$action['id'],
		'type'=>$type,
		'num'=>$num,
		'days'=>$days,
		'price'=>$fee,
		'createtime'=>TIMESTAMP,
		'content'=>"{$action['price']}元/{$action['days']}天",
		'remark'=>$old['time']?'续费':'购买',
	);
	pdo_insert('core_account_store_logs',$store_log);
	$log_id=pdo_insertid();
	$pay=array(
			'id'=>$log_id,
			'title'=>'商店订单',
			'fee'=>$store_log['price'],
			'back_url'=>'core/account.store/payresult',
		);
		$pay_id=cfc('core')->account_pay($pay);
		pdo_update('core_account_store_logs',array('pay_id'=>$pay_id),array('id'=>$log_id));
		exi('','','core/account.pay/main/id='.$pay_id);
	
}
if($_RQ['op']=='payresult'){
	//支付结果处理
	if(!$_SESSION['payresult']){
		exi('参数错误！','error','log');
	}
	$payresult=$_SESSION['payresult'];
	unset($_SESSION['payresult']);
	$store_log=pdo_get('core_account_store_logs',array('id'=>$payresult['id']));
	if(!$store_log){
		exi('订单不存在！','error');
	}
	if($store_log['price']!=$payresult['fee']){
		exi('支付出错，订单金额不匹配！','error','log');
	}
	if($store_log['status']==0){
		$old=pdo_get('core_account_permissions',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$store_log['pid'],'type'=>$store_log['type']),array('id','time'));
		if($old['time']){
			if($old['time']>TIMESTAMP){
				//exi($oldtime,'error');
				$time=$old['time']+$store_log['days']*86400;
			}else{
				$time=TIMESTAMP+$store_log['days']*86400;
			}
			pdo_update('core_account_permissions',array('time'=>$time),array('id'=>$old['id']));
		}else{
			$time=TIMESTAMP+$store_log['days']*86400;
			pdo_insert('core_account_permissions',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$store_log['pid'],'type'=>$store_log['type'],'time'=>$time));
		}
		
	}
	exi('购买成功！退出重新登陆后生效！','','list');
	
}
if($_RQ['op']=='buy_num'){
	$setting=core_setting('num_price');
	if(!$setting){
		exi('系统暂不支持应用次数单独购买，请联系管理员！','error');
	}
	$num_types=model('core/web')->num_types;
	$list=array();
	foreach($setting as $key=>$value){
		$list[]=array('title'=>$num_types[$key],'buy'=>1,'buy_action'=>array('op'=>'buy_num_edit','query'=>'type='.$key,'title'=>'购买'),'time'=>"{$value}元/个");
	}
	
	$return['tabs']=$tabs;
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='buy_num_edit'){
	$setting=core_setting('num_price');
	$num_types=model('core/web')->num_types;
	if($_RQ['postdata']){
		$post=get_postdata();
		$type=trim($post['type']);
		if(!$setting[$type]){
			exi('系统暂不支持该类型次数单独购买，请联系管理员！','error');
		}
		$buy_num=intval($post['buy_num']);
		$fee=round($buy_num*$setting[$type],2);
		$credit=pdo_getcolumn('core_accounts',array('uniacid'=>$_SESSION['uniacid']),'credit2');
		if($credit<$fee){
			exi('账户余额不足！请先充值！','error','core/account.recharge/');
		}
		$pay=array(
			'type'=>2,
			'is_add'=>0,
			'fee'=>$fee,
			'remark'=>"购买【{$num_types[$type]}个数】:{$buy_num}个",
		);
		model('core/web')->account_credit($pay);
		$check=pdo_getcolumn('core_account_nums',array('uniacid'=>$_SESSION['uniacid']),'uniacid');
		if(!$check){
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				$type=>$buy_num,
			);
			pdo_insert('core_account_nums',$insert);
		}else{
			$update=array("{$type} +="=>$buy_num);
			pdo_update('core_account_nums',$update);
		}
		exi('购买成功！','','goback');
	}
	$type=trim($_RQ['type']);
	
	if(!$setting[$type]){
		exi('系统暂不支持该类型次数单独购买，请联系管理员！','error');
	}
	
	if(!$num_types[$type]){
		exi('数量类型错误！','error');
	}
	$item['type']=$type;
	$item['now']=model('core/web')->get_buy_num($type);
	$item['group_now']=model('core/web')->get_group_num($type);
	$item['used']=model('core/web')->get_used_num($type);
	$item['price']=$setting[$type];
	$return['item']=$item;
	$return['num_types']=$num_types;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'radio','name'=>'type','title'=>'购买类型','options'=>'num_types','read'=>true),
			array('type'=>'string','name'=>'group_now','title'=>'套餐数量','read'=>true),
			array('type'=>'string','name'=>'now','title'=>'已购买数量','read'=>true),
			array('type'=>'string','name'=>'used','title'=>'已使用数量','read'=>true),
			array('type'=>'string','name'=>'price','title'=>'当前价格（元/个）','read'=>true),
			array('type'=>'string','name'=>'buy_num','title'=>'本次购买数量'),
		),
	);
	exi($return);
}